<?php
namespace Admin\Controller;

class FilesetController extends AdminController
{
    protected function _initialize()
    {
		parent::_initialize ();
	}

	public function add()
	{
		if (IS_POST)
		{
			$strFile_key   = trim($_POST['file_key']);
			$strE_file_key = trim($_POST['e_file_key']);

			if (empty($strFile_key))
			{
				$this->error('文件名不能为空');
			}

			if (empty($strE_file_key))
			{
				$this->error('字段名不能为空');
			}
			$_totalFinace = 0;
			foreach ($_POST['finance_range'] as $value)
			{
				$_totalFinace += $value;
			}

          	$data = [
				'file_key'      => $_POST['file_key'],
				'e_file_key'    => $_POST['e_file_key'],
				'bank_id'       => $_POST['bank_id'],
				'business_type' => $_POST['business_type'],
				'models_form'   => $_POST['models_form'],
				'license_type'  => $_POST['license_type'],
				'file_type'     => $_POST['file_type'],
				'create_time'   => time(),
				'sort'			=> $_POST['sort'],
				];

			$result = $this->updateData($data, 'file_set');
			if($result)
			{
				$this->success('添加成功',U('Fileset/index'));
			}else{
				return $this->error('添加失败');
			}
		}
		$_bankList   = $this->getList('*','bank','status = 0','id desc');
		$this->assign('business_type',$this->carFirmItems);
		$this->assign('models_form',$this->carModelItems);
		$this->assign('license_type',$this->onCardItems);
		$this->assign('file_type',$this->fileTypeItems);
		$this->assign('_bankList',$_bankList);
		$this->display();
	}

	public function edit()
	{
		if (IS_POST)
		{
          	$data = [
					'file_key'      => $_POST['file_key'],
					'e_file_key'    => $_POST['e_file_key'],
					'bank_id'       => $_POST['bank_id'],
					'business_type' => $_POST['business_type'],
					'models_form'   => $_POST['models_form'],
					'license_type'  => $_POST['license_type'],
					'file_type'     => $_POST['file_type'],
					'is_pic'        => $_POST['is_pic'],
					'create_time'   => time(),
					'sort'          => $_POST['sort'],
				];

	  		$result = M('file_set')->where('id='.I('post.id'))->save($data);
	  		if ($result)
	  		{
	  			$this->redirect('Fileset/index');
	  		}
		} else {
			$info        = $this->getInfo('*','file_set','id='.I('get.id',0,'intval'));
			$_bankList   = $this->getList('*','bank','status = 0','id desc');
			$this->assign('info',$info);
			$this->assign('_bankList',$_bankList);
			$this->assign('business_type',$this->carFirmItems);
			$this->assign('models_form',$this->carModelItems);
			$this->assign('license_type',$this->onCardItems);
			$this->assign('file_type',$this->fileTypeItems);
			$this->display('edit');
		}

	}

	public function index($p=1)
	{
		$p = intval($p) > 0 ? $p : 1;

        $fileSet = M('file_set as uf');
        $pagesize = 30;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量

		$where = '1=1';
		$where .= I('get.bank_id') != '' ? ' and uf.bank_id = ' . I('get.bank_id')  : '';
		$where .= I('get.models_form') != '' ? ' and uf.models_form = ' . I('get.models_form') : '';
		$where .= I('get.business_type') != '' ? ' and uf.business_type = ' .I('get.business_type') : '';
		$where .= I('get.license_type') != '' ? ' and uf.license_type = ' . I('get.license_type') : '';
		$where .= I('get.file_type') != '' ? ' and uf.file_type = ' . I('get.file_type') : '';
		$where .= ' and uf.status = 0 ';

		$count     = $fileSet->where($where)->count();
		$list      = $fileSet->field('uf.*,b.bank_name')->where($where)->join('join app_bank b on b.id = uf.bank_id')->order('bank_id desc,business_type,models_form,license_type')->limit($offset . ',' . $pagesize)->select();

		$page      = new \Think\Page($count, $pagesize);
		$page      = $page->show(1);
		$bankList  = $this->getList('*','bank','','');
		$listCount = count($list);

		$this->assign('listCount',$listCount);
		$this->assign('list',$list);
		$this->assign('business_type',$this->carFirmItems);
		$this->assign('bankList',$bankList);
		$this->assign('models_form',$this->carModelItems);
		$this->assign('license_type',$this->onCardItems);
		$this->assign('file_type',$this->fileTypeItems);
        $this->assign('page', $page);
		$this->display();
	}
}